package com.balance.anole.erp.report.view;

import java.util.List;

import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import com.balance.anole.common.service.CommonService;
import com.balance.anole.common.to.ReportMappingTO;
import com.balance.anole.erp.report.action.ReportOrderAnalyzeAction;
import com.balance.anole.erp.report.provider.ReporeTemplateLabelProvider;
import com.balance.anole.erp.util.Messages;
import com.balance.anole.erp.util.ServicesCenter;
import com.balance.anole.utils.Constants;

public class ReportOrderAnalyzeView extends ViewPart{
	public static final String ID = "com.balance.anole.erp.report.view.ReportOrderAnalyzeView";
	
	private Label label_Num;
	
	@Override
	public void createPartControl(Composite parent) {
		FormData formData;
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FormLayout());
		
		label_Num = new Label(container,SWT.RIGHT); 
		label_Num.setText(" - ");
		formData = new FormData();
		formData.right = new FormAttachment(100,-5);
		formData.bottom = new FormAttachment(100,-5);
		label_Num.setLayoutData(formData);
		
		Label label_recordNum = new Label(container,SWT.RIGHT); 
		label_recordNum.setText(Messages.VIEW_INFO_RECORDNUM);
		formData = new FormData();
		formData.right = new FormAttachment(label_Num,-5);
		formData.bottom = new FormAttachment(100,-5);
		label_recordNum.setLayoutData(formData);
		
		TableViewer tableViewer = new TableViewer(container, SWT.SINGLE|SWT.FULL_SELECTION);
		Table table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		formData = new FormData();
		formData.top = new FormAttachment(0,5);
		formData.bottom = new FormAttachment(label_recordNum,-10);
		formData.left =  new FormAttachment(0,5);
		formData.right =  new FormAttachment(100,-5);
		table.setLayoutData(formData);
		
		String[] columnNames = new String[]{Messages.TBL_REPORT_TEMPNAME,Messages.TBL_REPORT_TEMPFILE};
		
		int[] columnWidths = new int[]{400,100};
		int[] columnAlignments = new int[]{SWT.LEFT,SWT.LEFT};
		
		for(int i=0;i<columnNames.length;i++){
			TableColumn tableColumn = new TableColumn(table,columnAlignments[i]);
			tableColumn.setText(columnNames[i]);
			tableColumn.setWidth(columnWidths[i]);
		}
		
		final CommonService commonService = ServicesCenter.getServicesCenter().getService(CommonService.class);
		List<ReportMappingTO> l = commonService.listReportMappingTOByModuleId(Constants.REPORT_TYPE_ORDERANALY);
		
		label_Num.setText(l.size()+ "");

		WritableList wl = new WritableList(l,ReportMappingTO.class);

		tableViewer.setContentProvider(new ObservableListContentProvider());
		tableViewer.setLabelProvider(new ReporeTemplateLabelProvider());	
		tableViewer.setInput(wl);
	
		getSite().setSelectionProvider(tableViewer);
		createToolbarButtons(this);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

	private void createToolbarButtons(ReportOrderAnalyzeView view){
		ReportOrderAnalyzeAction reportOrderAnalyzeAction = new ReportOrderAnalyzeAction(view);
		this.getViewSite().getActionBars().getToolBarManager().add(reportOrderAnalyzeAction);
		
		reportOrderAnalyzeAction.setEnabled(true);
	}

	
	
}
